package com.jerry.secondkill.rabbitmq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jerry.secondkill.redis.RedisService;

@Service
public class MQSender {

	private static Logger log = LoggerFactory.getLogger(MQSender.class);
	
	@Autowired
	RedisService redisService;
	@Autowired
	AmqpTemplate amqpTemplate ;
	
	public void sendSeckillMessage(SeckillMessage mm) {
		String msg = redisService.beanToString(mm);
		log.info("send seckill message:"+msg);
		amqpTemplate.convertAndSend(MQConfig.QUEUE_SECKILL, msg);
	}
	
	public void sendDirect(Object message) {
		String msg = redisService.beanToString(message);
		log.info("send direct message:"+msg);
		amqpTemplate.convertAndSend(MQConfig.QUEUE, msg);
	}
	
	public void sendTopic(Object message) {
		String msg = redisService.beanToString(message);
		log.info("send topic message:"+msg);
		amqpTemplate.convertAndSend(MQConfig.EXCHANGE_TOPIC, "topic.key1", msg+"1");
		amqpTemplate.convertAndSend(MQConfig.EXCHANGE_TOPIC, "topic.key2", msg+"2");
	}
	
	public void sendFanout(Object message) {
		String msg = redisService.beanToString(message);
		log.info("send fanout message:"+msg);
		amqpTemplate.convertAndSend(MQConfig.EXCHANGE_FANOUT, "", msg);
	}
	
	public void sendHeader(Object message) {
		String msg = redisService.beanToString(message);
		log.info("send fanout message:"+msg);
		
		MessageProperties properties = new MessageProperties();
		properties.setHeader("header1", "value1");
		properties.setHeader("header2", "value2");
		Message obj = new Message(msg.getBytes(), properties);
		amqpTemplate.convertAndSend(MQConfig.EXCHANGE_HEADERS, "", obj);
	}

	
	
}
